<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
</head>
<body>
<div id="app" class="demo-input-size">

    <el-input
            size="medium"
            placeholder="请输入新闻栏目名称"
            suffix-icon="el-icon-date"
            style="display: inline-block;width: 200px"
            v-model="searchInfo.cName">
    </el-input>
    <el-button type="primary" round icon="el-icon-search" @click="searchNewsColumn()">
        搜索
    </el-button>
    <!--<el-button type="warning" round icon="el-icon-circle-plus-outline" @click="dialogFormVisible = true" disabled>-->
        <!--添加新闻栏目-->
    <!--</el-button>-->

    <el-table
            border
            :data="columnArr">
        <el-table-column
                fixed
                prop="cname"
                label="栏目名称">
        </el-table-column>
        <el-table-column
                fixed
                prop="cdesr"
                label="栏目描述">
        </el-table-column>
        <el-table-column
                fixed
                prop="cupdateDate"
                label="栏目修改时间">
        </el-table-column>
        <el-table-column
                fixed
                fixed="right"
                label="操作">
            <template slot-scope="scope">
                <el-button @click="showUpdateDialog(scope.row.id)" size="small" type="primary" icon="el-icon-edit" plain>
                    编辑
                </el-button>
                <!--<el-button @click="deleteNewsColumn(scope.row.id)" size="small" type="danger" icon="el-icon-delete" disabled>-->
                    <!--删除-->
                <!--</el-button>-->
            </template>
        </el-table-column>
    </el-table>
    <el-pagination
            @current-change="handleCurrentChange"
            layout="prev, pager, next"
            :total="totalCount">
    </el-pagination>

    <el-dialog title="添加新闻栏目" :visible.sync="dialogFormVisible">
        <el-form :model="newsColumnInfo" ref="newsColumnInfo" :rules="rules" >
            <el-form-item label="栏目名称" prop="cname">
                <el-input v-model="newsColumnInfo.cname"></el-input>
            </el-form-item>
            <el-form-item label="栏目描述" prop="cdesr">
                <el-input type="textarea" v-model="newsColumnInfo.cdesr"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="addNewsColumn()">添 加</el-button>
        </div>
    </el-dialog>

    <el-dialog title="修改新闻栏目" :visible.sync="updateDialogFormVisible">
        <el-form :model="newsColumnInfo" ref="newsColumnInfo" :rules="rules" >
            <el-form-item label="栏目名称" prop="cname">
                <el-input v-model="newsColumnInfo.cname"></el-input>
            </el-form-item>
            <el-form-item label="栏目描述" prop="cdesr">
                <el-input type="textarea" v-model="newsColumnInfo.cdesr"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="updateDialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="updateColumn()">确 定</el-button>
        </div>
    </el-dialog>

</div>
<script>

    var haha = new Vue({
        el:"#app",
        data:{
            searchInfo:{
                cName:'',
            },
            columnArr:[],
            totalCount:0,
            dialogFormVisible:false,
            updateDialogFormVisible:false,
            newsColumnInfo:{},
            rules:{
                cname: [
                    { required: true, message: '请输入栏目名称', trigger: 'blur' },
                    { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
                ],
                cdesr: [
                    { required: true, message: '请输入栏目描述', trigger: 'change' },
                ],
            }
        },
        methods:{
            updateColumn() {
                $.post("/demo/column/updateColumn",haha.newsColumnInfo,function (backData) {
                    if(backData.code == 1){
                        haha.dialogFormVisible = false;
                        haha.$refs["newsColumnInfo"].resetFields();
                        haha.$message.success('修改成功');
                        getData(1, 10);
                    }else{
                        haha.$message.error('修改失败 请确认数据 或者联系管理员');
                    }
                });
            },
            showUpdateDialog(id) {
                $.get("/demo/column/getColumn/" + id, function (backData) {
                    haha.newsColumnInfo = backData.data;
                    haha.updateDialogFormVisible = true;
                });
            },
            deleteNewsColumn(id) {
                haha.$confirm('此操作将永久删除该栏目, 是否继续?', '温馨提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    $.get("/demo/column/deleteColumn/" + id ,function (backData) {
                        haha.$message.success(backData.msg);
                        getData(1, 10);
                    })
                });
            },

            addNewsColumn() {
                haha.$refs["newsColumnInfo"].validate((valid) => {
                    if (valid) {
                        $.post("/demo/column/addColumn", haha.newsColumnInfo, function (backData) {
                            if(backData.code == 1){
                                haha.dialogFormVisible = false;
                                haha.$refs["newsColumnInfo"].resetFields();
                                haha.$message.success('添加成功');
                                getData(1,10);
                            }else{
                                haha.$message.error('添加失败 请确认数据 或者联系管理员');
                            }
                        });
                    }
                });
            },
            searchNewsColumn() {
                getData(1, 10);
            },
            handleCurrentChange( val  ) {
                getData(val,10);
            }
        }
    });
    function getData(a, b) {
        haha.searchInfo.npageNumber = a;
        haha.searchInfo.pageCount = b;
        $.post("/demo/column/listColumn", haha.searchInfo, function (backData) {
            haha.columnArr = backData.data.currentData;
            haha.totalCount = backData.data.totalCount;
        })
    }
    getData(1,10);

</script>
</body>
</html>